<?php
namespace mybackend\controllers;

use common\services\myhtsq\ErpInterfaceService;
use Yii;
use common\services\myhtsq\InvoiceService;
use common\models\myhtsq\InvoiceModel;
use common\error\ErrorCode;
use common\entities\myhtsq\ActionCode;
use yii\filters\AccessControl;

/**
 * Description of InvoiceController
 * 发票控制器
 * @author chenxc
 */
class InvoiceController extends BaseController
{

    private $_invoiceService;

    public function __construct($id, $module, InvoiceService $invoiceService, $config = array())
    {
        $this->_invoiceService = $invoiceService;
        parent::__construct($id, $module, $config);
    }

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['invoice-expired-remind'],
                        'allow' => true,
                    ],
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ]
        ];
    }

    protected function roleAuthRules()
    {
        return [
            'actions' => ['DeleteInvoice', 'DoLose', 'GetInvoiceInfo', 'GetInvoiceListByPage', 'SaveInvoice', 'ExportInvoice'],
            'rules' => [
                [
                    'allow' => true,
                    'actionCodeMaps' => [
                        'DeleteInvoice' => ActionCode::INVOICE_MNG,
                        'DoLose' => ActionCode::INVOICE_MNG,
                        'GetInvoiceInfo' => ActionCode::INVOICE_VIEW,
                        'GetInvoiceListByPage' => ActionCode::INVOICE_VIEW,
                        'SaveInvoice' => ActionCode::INVOICE_MNG,
                        'ExportInvoice' => ActionCode::INVOICE_MNG
                    ]
                ]
            ]
        ];
    }

    /**
     * 发票管理模块：获取发票列表信息，支持分页
     */
    public function actionGetInvoiceListByPage()
    {
        $invoiceModel = new InvoiceModel(['scenario' => 'getinvoicelistbypage']);
        if ($invoiceModel->paramsGetValidate()) {
            $params = $invoiceModel->getAttributes();
            $data = $this->_invoiceService->getInvoiceListByPage($params['is_main_invoice'], $params['invoice_status'], $params['invoice_begin_date'], $params['invoice_end_date'], $params['searchKey'], $params['pageIndex'], $params['pageSize']);

            return $this->json([
                'retCode' => ErrorCode::SUCCESS,
                'data' => $data
            ]);
        } else {
            Yii::error($invoiceModel->getLogMsg());
            return $this->json([
                'retCode' => $invoiceModel->getErrCode(),
                'errMsg' => $invoiceModel->getErrMsg(),
            ]);
        }
    }

    /**
     * 获取单个发票信息
     */
    public function actionGetInvoiceInfo()
    {
        $invoiceId = $this->getParam("invoiceId");
        $result = $this->_invoiceService->getInvoiceInfo($invoiceId);
        $this->exportJson($result, $this->_invoiceService->getErrCode(), $this->_invoiceService->getErrMsg());
    }

    /**
     * 保存发票
     */
    public function actionSaveInvoice()
    {
        $this->exitIfNotPost();
        $invoiceModel = new InvoiceModel(['scenario' => 'saveinvoice']);
        if ($invoiceModel->load(Yii::$app->request->post(), '')) {
            $result = $this->_invoiceService->saveInvoice($invoiceModel);
            $this->exportJson($result, $this->_invoiceService->getErrCode(), $this->_invoiceService->getErrMsg());
        } else {
            $this->errorParam();
        }
    }

    /**
     * 删除发票
     */
    public function actionDeleteInvoice()
    {
        $invoiceId = $this->getParam("invoiceId");
        $result = $this->_invoiceService->deleteInvoice($invoiceId);
        $this->exportJson($result, $this->_invoiceService->getErrCode(), $this->_invoiceService->getErrMsg());
    }

    /**
     * 标记遗失
     */
    public function actionDoLose()
    {
        $invoiceId = $this->getParam("invoiceId");
        $result = $this->_invoiceService->doLose($invoiceId);
        $this->exportJson($result, $this->_invoiceService->getErrCode(), $this->_invoiceService->getErrMsg());
    }

    /**
     * 获取发票列表（select2控件）
     * @param string $keyword 搜索关键字
     * @param string $page 分页
     */
    public function actionGetInvoiceListForSepcialInvoiceSelect2($keyword, $limit)
    {
        $result = $this->_invoiceService->getInvoiceListForSepcialInvoiceSelect2($keyword, $limit);
        $this->exportJson($result, $this->_invoiceService->getErrCode(), $this->_invoiceService->getErrMsg());
    }

    /**
     * 获取开票单位
     * @param string $keyword 搜索关键字
     * @return array
     */
    public function actionGetInvoiceBusinessUnit($keyword = '')
    {
        $data = $this->_invoiceService->getInvoiceBusinessUnit($keyword);
        $this->exportJson($data, $this->_invoiceService->getErrCode(), $this->_invoiceService->getErrMsg());
    }

    /*发送邮件提醒*/
    /**
     * 过期未全部回款的发票发送提醒
     */
    public function actionInvoiceExpiredRemind()
    {
        $this->_invoiceService->invoiceExpiredRemind();
    }

    /**
     * 导出发票excel
     */
    public function actionExportInvoice()
    {
        $is_main_invoice = $this->getParam('is_main_invoice');
        $invoice_status = $this->getParam('invoice_status');
        $isMainInvoice = empty($is_main_invoice) ? "-1" : $is_main_invoice;
        $invoiceStatus = empty($invoice_status) ? "-1" : $invoice_status;
        $invoiceBeginDate = $this->getParam('invoice_begin_date');
        $invoiceEndDate = $this->getParam('invoice_end_date');
        $searchKey = $this->getParam('searchKey');
        $pageIndex = $this->getParam('pageIndex');
        $pageSize = $this->getParam('pageSize');

        $data = $this->_invoiceService->exportInvoiceListExecl($isMainInvoice, $invoiceStatus, $invoiceBeginDate, $invoiceEndDate, $searchKey, $pageIndex, $pageSize);
        $this->exportJson($data, $this->_invoiceService->getErrCode(), $this->_invoiceService->getErrMsg());
    }

    /**
     * 导入发票
     * import-invoice-list-execl
     */
    public function actionImportInvoiceListExecl()
    {
        $data = $this->_invoiceService->importInvoiceListExecl();
        $this->exportJson($data, $this->_invoiceService->getErrCode(), $this->_invoiceService->getErrMsg());
    }
}
